package com.candy.control;

import com.candy.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //        接收前端传递进来的参数
        String username= req.getParameter("username");
        String userpwd= req.getParameter("userpwd");
        System.out.println("已接收到前端传递进来的信息，username="+username+";userpwd="+userpwd);
//    判断用户名密码是否正确
//        调用业务逻辑处理层
        LoginService service=new LoginService();
        boolean ok=service.login(username,userpwd);
        if(ok){
//            保存用户的信息，到session
            req.getSession().setAttribute("user",username);
//            匹配成功后，跳转到登录成功的页面上
            req.getRequestDispatcher("welcome.jsp").forward(req,resp);
        }else {
            resp.sendRedirect("index.jsp");
        }


    }
}
